<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
/* if (!isset($_SESSION['Username']) || !isset($_SESSION['Password'])){
  header('Location: index.php');


  } */

require("database/profile_handler.php");
require 'database/photo_handler.php';
require 'database/photo_uploader.php';
require 'database/wall_handler.php';
require 'database/follower_handler.php';
require 'database/notif_handler.php';

if (isset($_GET['UserTujuan'])) {
    $UserTujuan = $_GET['UserTujuan'];
    $t = new profile_handler();
    $t->get_nama_lengkap($UserTujuan);
} else
    header("Location: homepage.php");



if ($UserTujuan == $_SESSION['Username']) {
    header("Location: homepage.php");
}


if (isset($_POST['SubmitPosting']) && isset($_GET['posting']) && $_SESSION['Username'] != null) {
    $posting_text = $_POST['posting_text'];
    $wall_handler = new wall_handler();
    $wall_handler->post_wall($_SESSION['Username'], $UserTujuan, $posting_text);
}

if (isset($_POST['SubmitComment']) && isset($_GET['wallid']) && $_SESSION['Username'] != null) {
    $comment_text = $_POST['comment_text'];
    $wall_handler = new wall_handler();
    $wall_handler->add_comment($_GET['wallid'], $_SESSION['Username'], $comment_text);
}

if (isset($_GET['Follow']) && $_GET['Follow'] == 1) {
    $f_handler = new follower_handler();
    $n_handler = new notif_handler();

    $f_handler->add_follower($UserTujuan, $_SESSION['Username']);
    $n_handler->set_follow_notif($UserTujuan, $_SESSION['Username']);
}

if (isset($_GET['Unfollow']) && $_GET['Unfollow'] == 1) {
    $f_handler = new follower_handler();

    $f_handler->del_follower($UserTujuan, $_SESSION['Username']);
}

//notifikasi

$clicked = $_GET['firstview'];
$notif_id = $_GET['notifid'];
if ($clicked == 1) {
    $n = new notif_handler();
    //get id?
    $n->set_read($notif_id);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>

        <link rel="stylesheet" href="style/style.css" />

        <script type="text/javascript" src="script/validator.js"></script>
        <script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php echo $t->result[0]; ?>'s Profile</title>
    </head>
    <body onLoad="onloadPageChat()" >

        <div id="container">
            <div id="headerBody">
                <form id="Search" name="Search" method ="GET" action="search_result.php">

                    <input type="text" id="SearchBar" name="SearchBar"/>

                    <input type="submit" id="SearchOk" name="SearchOk" value="Search"/>
                </form>
<?php if ($_SESSION['Username'] != null) {
?>
                <a id="headerLink" href="javascript:viewHome();" id="headerLink">Home</a>
                <a id="headerLink" href="javascript:showPopupProfile();" id="headerLink">Profile</a>
                <a id="headerLink" href="javascript:viewMessage();" id="headerLink">Message</a>
                <a id="headerLink" href="javascript:viewUploader();" id="headerLink">Uploader</a>
                <a id="headerLink" href="javascript:viewGallery();" id="headerLink">Gallery</a>
                <a href="database/login.php?logout=1" id="logout_link" >Logout</a>
                <a href="chat.php" id="logout_link" >Chat</a>
                <a href="javascript:viewProfile();" id="logout_link" ><?php echo $_SESSION['Username'];?></a>
                <a href="javascript:showNotif('<?php echo $_SESSION['Username']; ?>');" id="headerLink">Notification</a>
                <!-- tampilin jumlah notif -->
<?php
                require_once 'database/notif_handler.php';

                $notif = new notif_handler();
                $jml = 0;

                $notif->retr_notification($_SESSION['Username']);
                if (count($notif->berita) > 0 && $notif->berita[0] != null) {
                    for ($i = 0; $i < count($notif->berita); $i++) {
                        if ($notif->read[$i] == 0) {
                            $jml++;
                        }
                    }
                    if ($jml > 0)
                        echo "<font color='red'>(" . $jml . ")</font>";
                }
                else {
                    echo "";
                }
?>
                <div id="profile_popup">
                </div>
                <div id="notif">
                    <div id="notif2">
                    </div>
                </div>
                <a href="profile.php" id="logout_link" ><?php echo $u; ?></a>
<?php } else { ?>
                <a href="index.php" id="logout_link" >Login</a>
<?php
            }
?>
            </div>
            <div id=""><img src="images/flickertiew_logo.gif" alt=""/></div>
            <!-- CONTENT dimulai disini -->
            <div id="centerBody">
                <div id ="content">
<?php
            $pf = new profile_handler();

            $pf->get_nama_lengkap($UserTujuan);
            $nama = $pf->result[0];

            $pr = new profile_handler();
            $pr->retr_details($UserTujuan, 5);

            $u = $UserTujuan;

            $ph = new photo_handler();

            //ambil profpic id
            $pf->retr_details($u, 4);
            $id = $pf->result[0];


            //ambil url
            $ph->get_url($id)
?>
                    <div id="col_otherprofile">
                        <div id="left_col">
                            <div id="top_content">
                                <div class="group_content" id="center_profile2">
                                    <span id="title_group"><?php echo $nama; ?>'s Profile</span>
                                    <div id="image_profile_center">
                                        <img src="<? echo $ph->result[0]; ?>" alt="" width="100"/>
                                    </div>
                                    <div id="profile_details2">
                                        <br />
                                        <div id="label_profile"> Full name</div> <div class="profile_content" id="FullnameSpan"> : <? echo $nama; ?></div>
                                        <div id="label_profile"> Email</div> <div class="profile_content" id="EmailSpan"> : <? echo $UserTujuan; ?></div>
                                        <div id="label_profile"> Birthday</div> <div class="profile_content" id="BirthdaySpan"> : <? echo $pr->result[0][0]; ?></div>
                                        <div id="label_profile"> Location</div> <div class="profile_content" id="LocationSpan"> : <? echo $pr->result[1][0]; ?></div>
                                        <div id="label_profile"> Hobby</div> <div class="profile_content" id="HobbySpan"> : <? echo $pr->result[2][0]; ?></div>
                                        <div id="label_profile"> Wall Privacy</div> <div class="profile_content" id="WallSpan"> : <? echo $pr->result[5][0]; ?></div>
                                    </div>
                                </div>
                            </div>
<?php
                    if (isset($_SESSION['Username'])) {
?>
                            <div id="content_two">
                                <div class="group_content" id="post_awall">
<?php
                        if ($pr->result[5][0] == "only me") {
                            $show_wallpost = false;
                        } else if ($pr->result[5][0] == "just followers") {
                            $fl_handler = new follower_handler();
                            if ($fl_handler->is_follow($_SESSION['Username'], $UserTujuan)) {
                                $show_wallpost = true;
                            } else {
                                $show_wallpost = false;
                            }
                        } else {
                            $show_wallpost = true;
                        }
                        if ($show_wallpost) {
?>
                                    <form method="post" action="otherprofile.php?UserTujuan=<?php echo $UserTujuan; ?>&posting=1">
                                        <div id="insert_post2">
                                            <textarea class="textarea" id="posting_text2" name="posting_text" rows="3" onFocus="if (this.value=='Post Something') { this.value = '';}" onBlur="if (this.value=='') { this.value = 'Post Something';}">Post Something</textarea>
                                            <input type="submit" name="SubmitPosting" id="SubmitPosting2" value="Submit"/>
                                        </div>
                                    </form>
<?php
                                } else {
?>
                                    No Post can be displayed.
<?php
                                }
?>
                                </div>
                            </div>
<?php
                            }
?>
                        </div>
                        <div id="right_col">
                            <div class="group_content" id="menu_profile">
<?php
                            if ($_SESSION['Username'] != null) {
                                $t_f = new follower_handler();
                                if (!$t_f->is_follow($_SESSION['Username'], $UserTujuan)) {
?>
                                    <span id="_title2"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $UserTujuan; ?>&Follow=1">Follow</a></span><br />
<?php
                                } else {
?>
                                    <span id="_title2"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $UserTujuan; ?>&Unfollow=1">Unfollow</a></span><br />
<?php
                                }
?>
                                <span id="_title2"><a id="link_name" href="message.php?UserTujuan=<?php echo $UserTujuan; ?>">Send Message</a></span><br />
<?php
                            }
?>
                                <span id="_title2"><a id="link_name" href="gallery.php?UserTujuan=<?php echo $UserTujuan; ?>">View Photos</a></span><br />
                            </div>

                            <div class="group_content" id="follower">
                                <div id="title_group">Follower</div>
<?php
                                $follower = new follower_handler();
                                $follower->retr_follower($UserTujuan);
                                if ($follower->result[0] != NULL) {
                                    for ($l = 0; $l < count($follower->result); $l++) {
                                        $photo_handler = new photo_handler();
                                        $prof_handler = new profile_handler();
                                        $prof_handler->retr_details($follower->result[$l], 4);
                                        $photo_handler->get_url($prof_handler->result[0]);
?><a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l] ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="image" id="image_follow" /></a>
                                <?php
                                    }
                                } else {
                                    echo "<br />";
                                }
                                ?>
                            </div>
                            <div class="group_content" id="following">
                                <div id="title_group">Following</div>
<?php
                                $follower = new follower_handler();
                                $follower->retr_following($UserTujuan);
                                if ($follower->result[0] != NULL) {
                                    for ($l = 0; $l < count($follower->result); $l++) {
                                        $photo_handler = new photo_handler();
                                        $prof_handler = new profile_handler();
                                        $prof_handler->retr_details($follower->result[$l], 4);
                                        $photo_handler->get_url($prof_handler->result[0]);
?><a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l] ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="image" id="image_follow" /></a>
                                <?php
                                    }
                                } else {
                                    echo "<br />";
                                }
                                ?>
                            </div>
                        </div>
<?php
                                if ($show_wallpost) {
?>
                                    <div class="group_content" id="group_wall">
                                        <div id="wall_post">
<?php
                                    $wall = new wall_handler();
                                    $wall->retr_post_byUsr($u);
                                    $res = $wall->result;

                                    if (count($wall->result) != 0) {

                                        //disini batasin wall kyk di homepage
                                         if (count($wall->result) <= 3) {
                                                $c = count($wall->result);
                                               // echo count($wall->result);
                                            }
                                            else {
                                                $c = 3;
                                            }

                                        for ($i = 0; $i < $c; $i++) {
                                            $parsed[$i] = $wall->parse_wall($res[$i]);
                                            $tag = $wall->parse_walltagged($parsed[$i]);
                                            $commentator = $wall->parse_wallcommentator($parsed[$i]);
                                            $comment_body = $wall->parse_wallcommbody($parsed[$i]);

                                            //for retrieving user posted
                                            $userposted = $wall->result_usrposted[$i];
                                            $profile_usrposted = new profile_handler();
                                            $profile_usrposted->retr_details($userposted, 5);
                                            $photo_usrposted = new photo_handler();
                                            $photo_usrposted->get_url($profile_usrposted->result[3][0]);
?>
                                            <div class="wall_container">
                                                <img id="profpic_small" src="<? echo $photo_usrposted->result[0]; ?>" alt="" />
                                                <div id="wall_name"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $userposted; ?>"><?php echo $profile_usrposted->result[4][0]; ?></a></div>
                                                <div id="wall_delete">
                                                    <?php
                                                    $nmc = $userposted;

                                                    if ($nmc == $_SESSION['Username']){
                                                ?>

                                                    <a href="home.php?delete_wall=<?php echo $wall->result_wallid[$i]; ?>&other=<?php echo $UserTujuan; ?>">x</a>

                                                    <?php

                                                    }
                                                    ?>
                                                </div>

                                                <div id="wall_body">
<?php
                                            $cekdulu = strstr($parsed[$i][0], "@\"");
                                            if (!$cekdulu) {
                                                // echo wall post
                                                echo $parsed[$i][0];
                                            } else {
                                                // disini parsing tagging
                                                // Masih Nge BUG, cek lagi ya??? :D
                                                // Kalo tag nya salah jadi salah semua..
                                                $masuk_tag = false;
                                                $end_tag = false;
                                                $tambah_akhir = true;
                                                for ($idx = 0; $idx < strlen($parsed[$i][0]) - 1; $idx++) {
                                                    if ($parsed[$i][0][$idx] == '@' && $parsed[$i][0][$idx + 1] == '"' && !$masuk_tag) {
                                                        $nama = "";
                                                        $masuk_tag = true;
                                                        $idx+=2;
                                                        if ($idx >= strlen($parsed[$i][0])) {
                                                            $masuk_tag = false;
                                                            $idx-=2;
                                                        }
                                                    } else if ($parsed[$i][0][$idx] == '"' && $masuk_tag) {
                                                        $masuk_tag = false;
                                                        $end_tag = true;
                                                    } else if (!$masuk_tag) {
                                                        $hasil_parsing .= $parsed[$i][0][$idx];
                                                    }

                                                    if ($masuk_tag) {
                                                        $nama .= $parsed[$i][0][$idx];
                                                        //echo $parsed[$i][0][$idx];
                                                    }
                                                    if ($idx == (strlen($parsed[$i][0]) - 2) && $masuk_tag && $parsed[$i][0][$idx + 1] == '"') {
                                                        $masuk_tag = false;
                                                        $end_tag = true;
                                                        $tambah_akhir = false;
                                                    } else if ($idx == (strlen($parsed[$i][0]) - 2) && $masuk_tag && $parsed[$i][0][$idx + 1] != '"') {
                                                        $end_tag = false;
                                                        $tambah_akhir = false;
                                                        $hasil_parsing = $parsed[$i][0];
                                                    }

                                                    if ($end_tag) {
                                                        $query = "SELECT usr_email FROM user WHERE nama_lengkap='$nama'";
                                                        $prof_handler2 = new profile_handler();
                                                        $prof_handler2->exec_q($query, 'usr_email');
                                                        if ($prof_handler2->result[0] != NULL) {
                                                            $hasil_parsing .= "<a id='link_name' href='otherprofile.php?UserTujuan=";
                                                            $hasil_parsing .= $prof_handler2->result[0];
                                                            $hasil_parsing .= "'>";
                                                            $hasil_parsing .= $nama;
                                                            $hasil_parsing .= "</a>";
                                                        } else {
                                                            $tambah_akhir = false;
                                                            $hasil_parsing = $parsed[$i][0];
                                                            $idx = strlen($parsed[$i][0]) - 1;
                                                        }
                                                        $end_tag = false;
                                                    }
                                                }
                                                if ($tambah_akhir)
                                                    $hasil_parsing .= $parsed[$i][0][$idx];
                                                echo $hasil_parsing;
                                                $hasil_parsing = "";
                                            }
                                            if (count($commentator) > 3) {
?>
                                                <div id="view_more_com<?php echo "_" . $i; ?>" align="center">
                                                    <!-- tambahin commentator[$i], tambahin comment_body jg?? pake POST aja apa? -->
<?php
                                                //parsing dulu
                                                $comm_str = "";
                                                for ($j = 0; $j < count($commentator); $j++) {
                                                    if ($commentator[$j] != null) {
                                                        $comm_str = $comm_str . $commentator[$j] . ";";
                                                    }
                                                }
                                                //edan panjang juga ya
                                                $comm_body = "";
                                                for ($j = 0; $j < count($comment_body); $j++) {
                                                    if ($comment_body[$j] != null) {
                                                        $comm_body = $comm_body . $comment_body[$j] . ";";
                                                    }
                                                }

                                                //for ($j=0; $j<count($commentator); $j++){
                                                //    echo $comm_str;
                                                //}
?>
                                                <a href="javascript:viewMoreWallComment(<?php echo $wall->result_wallid[$i]; ?>,<?php echo count($commentator) - 1; ?>,<?php echo $i; ?>, '<?php echo $comm_str; ?>','<?php echo $comm_body; ?>','<?php echo $UserTujuan; ?>')" id="headerLink">view more comments...</a>
                                            </div>


<?php
                                            }
?>

                                        </div>
<?php
                                            // echo comment
                                            //dibatesin tiga juga kyk di homepage

                                            for ($j = 0; $j < 3; $j++) {
                                                // echo commentator
                                                if (strlen($comment_body[$j]) > 0) {
                                                    $commentator_prof = new profile_handler();
                                                    $commentator_prof->retr_details($commentator[$j], 5);
                                                    $photo_handler = new photo_handler();
                                                    $photo_handler->get_url($commentator_prof->result[3][0]);
?>
                                                    <div id="wall_comment2">
                                                        <img id="image_commentator" src="<?php echo $photo_handler->result[0]; ?>" alt="" />
                                                        <div id="name_commentator"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $commentator[$j] ?>">
<?php
                                                    echo $commentator_prof->result[4][0];
?>
                                                </a>
                                            </div>
                                              <div id="comment_delete">
                                                <?php
                                                    if ($commentator[$j] == $_SESSION['Username']){
                                                ?>
                                                <a href="home.php?comment_wallid=<?php echo $wall->result_wallid[$i]; ?>&commentator=<?php echo $commentator[$j]; ?>&comment=<?php echo $comment_body[$j]; ?>">x</a>
                                            <?php
                                                    }
                                            ?>
                                            </div>
                                            <div id="comment_content2">
<?php
                                                    // echo comment body
                                                    echo $comment_body[$j];
?>
                                                </div>
                                            </div>
<?php
                                                }
                                            }
                                            if (isset($_SESSION['Username'])) {
?>
                                                <div id="more_comments<?php echo "_" . $i; ?>">

                                                </div>
                                                <form method="post" action="otherprofile.php?UserTujuan=<?php echo $UserTujuan . '&wallid=' . $wall->result_wallid[$i]; ?>">
                                                    <div id="insert_comment">
                                                        <textarea class="textarea" name="comment_text" size="50px" rows="2"></textarea>
                                                        <input type="submit" name="SubmitComment" id="SubmitComment" value="Submit"/>
                                                    </div>
                                                </form>
<?php
                                            }
?>
                                        </div>
                                    <?php
                                        }
                                        if (count($wall->result) > 3) {
                                    ?>
                                            <div id="more_posts">
                                            </div>
                                            <div id="view_more" align="center">
                                                <a href="javascript:viewMoreWallPost(<?php echo count($wall->result); ?>,'<?php echo $UserTujuan; ?>')" id="headerLink">view more posts...</a>
                                            </div>

<?php
                                        }
                                    } else {
                                        echo "Wall is empty";
                                    }
?>
                                </div>
                                <?php
                                }
                                ?>
                        </div>

                    </div>
                </div>
            </div>
            <div id="footerBody" class="group_content">
                <div id="footer" align="center">
                    Copyright &copy; 2011<br/>
                    Created by <a href="http://www.facebook.com/Gunawan.Robert" id="headerLink">Robert Gunawan</a>
                    , <a href="http://www.facebook.com/danz3m" id="headerLink">Danang Tri Massandy</a>,
                    <a href="http://www.facebook.com/achmad.giovani" id="headerLink">Achmad Giovani</a>
                </div>
            </div>
        </div>


    </body>
</html>

